package com.lottery.im.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.lottery.im.R;

/**
 * Created by lianwufeng on 2017/10/9.
 * 单行 输入 对话框
 */

public class EditDialogBuilder extends Dialog {

  private static Context tmpContext;
  private static EditDialogBuilder instance;

  private View mDialogView;
  private RelativeLayout mLayoutAll;

  private TextView mTvTitle;
  private EditText mEtContent;
  private Button mBtnCancel;
  private Button mBtnConfirm;

  private View.OnClickListener mOnCancelClick;
  private onEditClickListener mOnEditClickListener;

  public EditDialogBuilder(Context context) {
    super(context);
    init(context);
  }

  public EditDialogBuilder(Context context, int theme) {
    super(context, theme);
    init(context);
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    WindowManager.LayoutParams params = getWindow().getAttributes();
    //params.height = ViewGroup.LayoutParams.MATCH_PARENT;
    params.width = ViewGroup.LayoutParams.MATCH_PARENT;
    getWindow().setAttributes(params);
    setCancelable(true);
    setCanceledOnTouchOutside(true);
  }

  public static EditDialogBuilder getInstance(Context context) {

    if (instance == null || !tmpContext.equals(context)) {
      synchronized (EditDialogBuilder.class) {
        if (instance == null || !tmpContext.equals(context)) {
          instance = new EditDialogBuilder(context, R.style.dialog_untran);
        }
      }
    }
    tmpContext = context;
    return instance;
  }

  private void init(Context context) {

    mDialogView = View.inflate(context, R.layout.dialog_edit, null);
    mLayoutAll = mDialogView.findViewById(R.id.layout_all);

    setContentView(mDialogView);

    //this.setOnShowListener(new OnShowListener() {
    //  @Override
    //  public void onShow(DialogInterface dialogInterface) {
    //    BaseEffects animator = new ZoomEffects();
    //    animator.start(mLayoutAll);
    //  }
    //});

    mLayoutAll.setOnClickListener(view -> dismiss());

    mDialogView.findViewById(R.id.layout_dialog).setOnClickListener(view -> {
    });

    mTvTitle = mDialogView.findViewById(R.id.tv_title);
    mEtContent = mDialogView.findViewById(R.id.et_content);
    mBtnCancel = findViewById(R.id.btn_cancel);
    mBtnConfirm = findViewById(R.id.btn_confirm);
    mBtnCancel.setOnClickListener(view -> {
      if (null != mOnCancelClick) {
        mOnCancelClick.onClick(view);
      }
      dismiss();
    });

    mBtnConfirm.setOnClickListener(view -> {
      if (null != mOnEditClickListener) {
        mOnEditClickListener.onEditClick(mEtContent.getText().toString());
      }
      dismiss();
    });
  }

  public EditDialogBuilder setTitle(String title) {
    mTvTitle.setText(title);
    return this;
  }

  public EditDialogBuilder setHint(String content) {
    mEtContent.setHint(content);
    return this;
  }

  public EditDialogBuilder setCancel(String content, View.OnClickListener onClickListener) {
    mBtnCancel.setText(content);
    this.mOnCancelClick = onClickListener;
    return this;
  }

  public EditDialogBuilder setConfirm(String content, onEditClickListener onEditClickListener) {
    mBtnConfirm.setText(content);
    this.mOnEditClickListener = onEditClickListener;
    return this;
  }

  @Override public void dismiss() {
    super.dismiss();
    instance = null;
    tmpContext = null;
  }

  @Override
  public void show() {
    super.show();
  }

  public interface onEditClickListener {
    void onEditClick(String content);
  }
}
